syntax = "proto3";
import "google/protobuf/struct.proto";

enum ContentMimeType {
  TEXT = 0; // text
  JSON = 1; // json
  HTML = 2; // html
  PNG = 3; // png
  JPEG = 4; // jpeg
  SVG = 5; // svg
  PDF = 6; // pdf
  LATEX = 7; // latex
}

message Content {
  ContentMimeType mime_type = 1; // mime type of content
  bytes data = 2; // data of content
}

enum BrowserCommandType {
  GOTO = 0; // go to url
  TERMINATE = 1; // terminate session
  WAIT = 2; // wait for selector
  CLICK = 3; // click selector
  COPY = 4; // copy selector
  TYPE = 5; // type selector
  SCROLL_X = 6; // scroll horizontal
  SCROLL_Y = 7; // scroll vertical
  ENTER = 8; // enter
}

message BrowserInput {
  BrowserCommandType type = 1; // command type
  string selector = 2; // selector to use
  string data = 3; // command data
}

message BrowserInitData {
  string url = 1; // url to open
  string terminate_url_pattern = 2; // url pattern to terminate session
  int32 timeout = 3; // timeout in seconds
  bool persist_session = 4; // persist session after done
  string session_data = 5; // session data to restore
}

message BrowserOutput {
  string url = 1; // url of page
  string text = 2; // copied text
}

message BrowserButton {
  string selector = 1; // selector of button
  string text = 2; // text of button
}

message BrowserInputField {
  string selector = 1; // selector of input
  string text = 2; // text of input
}

message BrowserSelectField {
  string selector = 1; // selector of select
  string text = 2; // text of select
}

message BrowserTextAreaField {
  string selector = 1; // selector of textarea
  string text = 2; // text of textarea
}

message BrowserLink {
  string selector = 1; // selector of link
  string text = 2; // text of link
  string url = 3; // url of link
}

message BrowserContent {
  string url = 1; // url of page
  string title = 2; // title of page
  string html = 3; // html of page
  string text = 4; // text of page
  bytes screenshot = 5; // screenshot of page if available
  repeated BrowserButton buttons = 6; // buttons on page
  repeated BrowserInputField inputs = 7; // inputs on page
  repeated BrowserSelectField selects = 8; // selects on page
  repeated BrowserTextAreaField textareas = 9; // textareas on page
  repeated BrowserLink links = 10; // links on page
  string error = 11; // error if any
}

message RemoteBrowserRequest {
  BrowserInitData init_data = 1; // init data
  BrowserInput input = 5; // input to send to browser
}

enum RemoteBrowserState {
  RUNNING = 0; // browser is running
  TERMINATED = 1; // browser is terminated
  TIMEOUT = 2; // browser is timed out
}

message RemoteBrowserSession {
  string ws_url = 1; // websocket url to connect to for remote display
  string session_data = 2; // session data to persist
}

message RemoteBrowserResponse {
  RemoteBrowserSession session = 1; // session data
  RemoteBrowserState state = 2; // state of browser
}

message PlaywrightBrowserRequest {
  string url = 1; // url to open
  repeated BrowserInput steps = 2; // List of inputs to send to browser
  int32 timeout = 3; // timeout in seconds
  string session_data = 4; // session data to restore
  bool stream_video = 5; // whether to stream video
}

message PlaywrightBrowserResponse {
  RemoteBrowserSession session = 1; // session data
  bytes video = 2; // video data
  RemoteBrowserState state = 3; // state of browser
  repeated BrowserOutput outputs = 4; // List of outputs from browser
  BrowserContent content = 5; // content of page
}

message PythonCodeRunnerFile {
  string name = 1; // name of file
  bytes content = 2; // content of file
}

message RestrictedPythonCodeRunnerRequest {
  string source_code = 1; // python code to run
  google.protobuf.Struct input_data = 2; // input data to pass to code
  int32 timeout_secs = 3; // timeout in seconds
}

message RestrictedPythonCodeRunnerResponse {
  RemoteBrowserState state = 1; // state of runner
  google.protobuf.Struct local_variables = 2; // local variable after running code
  repeated Content stdout = 3; // stdout of code
  string stderr = 4; // stderr of code
  int32 exit_code = 5; // exit code of code
}

// Generic runner service
service Runner {

  rpc GetRemoteBrowser (stream RemoteBrowserRequest) returns (stream RemoteBrowserResponse) {}

  rpc GetPlaywrightBrowser (stream PlaywrightBrowserRequest) returns (stream PlaywrightBrowserResponse) {}

  rpc GetRestrictedPythonCodeRunner (RestrictedPythonCodeRunnerRequest) returns (stream RestrictedPythonCodeRunnerResponse) {}
}